﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.DirectoryServices.AccountManagement;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

namespace VRM2__2012_
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow
  {
    StackPanel deliverySchedule = new StackPanel();
    StackPanel resourcePlan = new StackPanel();
    StackPanel userSettings = new StackPanel();
    SqlConnection myConnection;
    SqlDataReader myReader = null;
    
    public MainWindow()
    {
      InitializeComponent();
    }

    #region menus

    private void header_MouseEnter(object sender, MouseEventArgs e)
    {
      VRM2CustomControls.menuHeader menHeader = (VRM2CustomControls.menuHeader)sender;

      SolidColorBrush brush = new SolidColorBrush();

      ColorAnimation colAnimation = new ColorAnimation();
      colAnimation.From = Colors.Transparent;
      colAnimation.To = Colors.Gray;
      colAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));

      brush.BeginAnimation(SolidColorBrush.ColorProperty, colAnimation);
      menHeader.headerForeground = brush;
    }

    private void header_MouseLeave(object sender, MouseEventArgs e)
    {
      VRM2CustomControls.menuHeader menHeader = (VRM2CustomControls.menuHeader)sender;

      SolidColorBrush brush = new SolidColorBrush();

      ColorAnimation colAnimation = new ColorAnimation();
      colAnimation.From = Colors.Gray;
      colAnimation.To = Colors.Transparent;
      colAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));

      brush.BeginAnimation(SolidColorBrush.ColorProperty, colAnimation);
      menHeader.headerForeground = brush;
    }

    private void header_MouseDown(object sender, MouseEventArgs e)
    {
      VRM2CustomControls.menuHeader menHeader = (VRM2CustomControls.menuHeader)sender;

      StackPanel panel = (StackPanel)menHeader.Parent;

      if (menHeader.headerOption == "Click To Collapse")
      {
        menHeader.headerOption = "Click To Expand";
        DoubleAnimation expand = new DoubleAnimation();
        expand.From = panel.ActualHeight;
        expand.To = menHeader.ActualHeight;
        expand.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        panel.BeginAnimation(StackPanel.HeightProperty, expand);
      }
      else
      {
        menHeader.headerOption = "Click To Collapse";
        DoubleAnimation expand = new DoubleAnimation();
        expand.From = menHeader.ActualHeight;
        expand.To = panel.ActualHeight;
        expand.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        panel.BeginAnimation(StackPanel.HeightProperty, expand);
      }
    }

    private void buttons_MouseEnter(object sender, MouseEventArgs e)
    {
      VRM2CustomControls.menuButton button = (VRM2CustomControls.menuButton)sender;

      if (button.IsPressed != true)
      {
        SolidColorBrush animBrush = new SolidColorBrush();

        ColorAnimation BackgroundAnimation = new ColorAnimation();

        BackgroundAnimation.From = Colors.Transparent;
        BackgroundAnimation.To = Color.FromRgb(21, 133, 181);
        BackgroundAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        animBrush.BeginAnimation(SolidColorBrush.ColorProperty, BackgroundAnimation);
        button.Background = animBrush;
      }
    }

    private void buttons_MouseLeave(object sender, MouseEventArgs e)
    {
      VRM2CustomControls.menuButton button = (VRM2CustomControls.menuButton)sender;

      if (button.IsPressed != true)
      {
        SolidColorBrush animBrush = new SolidColorBrush();

        ColorAnimation BackgroundAnimation = new ColorAnimation();

        BackgroundAnimation.From = Color.FromRgb(21, 133, 181);
        BackgroundAnimation.To = Colors.Transparent;
        BackgroundAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        animBrush.BeginAnimation(SolidColorBrush.ColorProperty, BackgroundAnimation);
        button.Background = animBrush;
      }
    }

    private void buttons_Click(object sender, RoutedEventArgs e)
    {
      closeAll(true);

      VRM2CustomControls.menuButton menbutton = (VRM2CustomControls.menuButton)sender;

      StackPanel panel = (StackPanel)menbutton.Parent;
      StackPanel panel2 = (StackPanel)panel.Parent;

      uncheckAll(panel2);

      SolidColorBrush brsh = new SolidColorBrush(Color.FromRgb(21, 133, 181));
      menbutton.Background = brsh;
      menbutton.MouseEnter -= buttons_MouseEnter;
      menbutton.MouseLeave -= buttons_MouseLeave;

      if (menbutton.Arrow == ">")
      {
        switch (menbutton.Tag.ToString())
        {
          case "1":
            if (resourceplanMenu.Width > 0)
              hideSubMenu(resourceplanMenu);

            if (userSettingMenu.Width > 0)
              hideSubMenu(userSettingMenu);

            openMenu(deliveryScheduleMenu, sender);
            break;

          case "2":
            if (deliveryScheduleMenu.Width > 0)
            {
              hideSubMenu(deliveryScheduleMenu);
              uncheckAll(deliveryScheduleStack);
            }

            if (userSettingMenu.Width > 0)
              hideSubMenu(userSettingMenu);

            openMenu(resourceplanMenu, sender);
            break;

          case "6":
            if (deliveryScheduleMenu.Width > 0)
            {
              hideSubMenu(deliveryScheduleMenu);
              uncheckAll(deliveryScheduleStack);
            }

            if (resourceplanMenu.Width > 0)
              hideSubMenu(resourceplanMenu);

            openMenu(userSettingMenu, sender);
            break;
        }
      }
      else
      {
        if (deliveryScheduleMenu.Width > 0)
          hideSubMenu(deliveryScheduleMenu);
        
        if(resourceplanMenu.Width > 0)
          hideSubMenu(resourceplanMenu);
      }

    }

    private void openMenu(ScrollViewer view, object sender)
    {
      var element = sender as ContentControl;
      if (element != null)
      {
        Point relativePoint = element.TransformToAncestor(this)
                          .Transform(new Point(0, 0));

        //var location = element.PointToScreen(new Point(0, 0));
        view.Margin = new Thickness(-5, relativePoint.Y - element.ActualHeight + 4, 0, 0);

        
      }

      openSubMenu(view);
    }

    public void uncheckAll(StackPanel sP)
    {
      StackPanel panel2 = sP;

      foreach (StackPanel pan in panel2.Children)
      {
        foreach (object obje in pan.Children)
        {
          if (obje.GetType().ToString() == "VRM2CustomControls.menuButton")
          {
            VRM2CustomControls.menuButton but = (VRM2CustomControls.menuButton)obje;
            SolidColorBrush brush = new SolidColorBrush(Colors.Transparent);
            but.Background = brush;

            but.MouseEnter -= buttons_MouseEnter;
            but.MouseEnter += buttons_MouseEnter;
            but.MouseLeave -= buttons_MouseLeave;
            but.MouseLeave += buttons_MouseLeave;
          }
        }
      }
    }

    public void openSubMenu(ScrollViewer viewer)
    {
      DoubleAnimation anim = new DoubleAnimation();
      anim.From = 0;
      anim.To = 257;
      anim.Duration = new Duration(TimeSpan.FromSeconds(0.5));
      viewer.BeginAnimation(ScrollViewer.WidthProperty, anim);
    }

    public void hideSubMenu(ScrollViewer viewer)
    {
      if (viewer.Width > 0)
      {
        DoubleAnimation anim = new DoubleAnimation();
        anim.From = 257;
        anim.To = 0;
        anim.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        viewer.BeginAnimation(ScrollViewer.WidthProperty, anim);
      }
    }

    private void closeAll(bool sub)
    {
      if (grpSettings.Width > 0)
        grpFromRight(grpSettings, 1270, 0);

      if (showWhich.Width > 0)
        grpFromRight(showWhich, 770, 0);

      if (userSettingsWindow.Width > 0)
        grpFromRight(userSettingsWindow, 770, 0);

      if ((resourceplanMenu.Width > 0) && (sub == true))
      {
        uncheckAll(menuStack);
        hideSubMenu(resourceplanMenu);
      }

      if ((deliveryScheduleMenu.Width > 0) && (sub == true))
      {
        uncheckAll(menuStack);
        hideSubMenu(deliveryScheduleMenu);
      }
    }

    private void grpFromRight(GroupBox box, int from, int to)
    {
      DoubleAnimation anim = new DoubleAnimation();
      anim.From = from;
      anim.To = to;
      anim.Duration = new Duration(TimeSpan.FromSeconds(0.5));
      box.BeginAnimation(GroupBox.WidthProperty, anim);
    }

  #endregion

    private void MetroWindow_Loaded_1(object sender, RoutedEventArgs e)
    {
      myConnection = new SqlConnection("Data Source= UKSTOL012\\SQLEXPRESS;Initial Catalog= test;Integrated Security=true");  

      int access = 0;

      this.Title = "Volume Resource Management";

      //username.Header = "Something";
      username.Header = Convert.ToString(UserPrincipal.Current.DisplayName);

      if(access != 0)
      {
        mnuAdministration.Visibility = Visibility.Hidden;
      }

      for (int i = 0; i < 10; i++)
      {
        string date = DateTime.Today.AddDays(i).ToLongDateString();
        StackPanel forDate = new StackPanel();

        VRM2CustomControls.menuHeader dHead = new VRM2CustomControls.menuHeader();
        dHead.headerText = date;
        dHead.headerOption = "Click To Expand";
        dHead.MouseEnter += header_MouseEnter;
        dHead.MouseLeave += header_MouseLeave;
        dHead.MouseDown += header_MouseDown;
        forDate.Children.Add(dHead);
        
        for(int j = 1; j < 10; j++)
        {
          VRM2CustomControls.menuButton dButton = new VRM2CustomControls.menuButton();
          dButton.Text = "HFB " + j;
          dButton.MouseEnter += buttons_MouseEnter;
          dButton.MouseLeave += buttons_MouseLeave;
          dButton.Tag = date;
          dButton.Click += menuButton_Click_1;
          forDate.Children.Add(dButton);
        }
        deliveryScheduleStack.Children.Add(forDate);
      }
      deliveryScheduleMenu.Content = deliveryScheduleStack;

      foreach (StackPanel s in deliveryScheduleStack.Children)
      {
        s.Height = 30;
      }


      for (int i = 35; i < 53; i++)
      {
        VRM2CustomControls.menuButton but = new VRM2CustomControls.menuButton();
        but.Text = "Week " + i.ToString();
        but.MouseEnter += buttons_MouseEnter;
        but.MouseLeave += buttons_MouseLeave;
        //but.Click += menuButton_Click_1;
        resourcePlan.Children.Add(but);
      }

      for (int i = 1; i < 36; i++)
      {
        VRM2CustomControls.menuButton but = new VRM2CustomControls.menuButton();
        but.Text = "Week " + i.ToString();
        but.MouseEnter += buttons_MouseEnter;
        but.MouseLeave += buttons_MouseLeave;
        //but.Click += menuButton_Click_1;
        resourcePlan.Children.Add(but);
      }
      resourceplanMenu.Content = resourcePlan;

      try
      {
        myConnection.Open();
        SqlCommand myCommand = new SqlCommand("SELECT username FROM users ", myConnection);
        myReader = myCommand.ExecuteReader();

        while (myReader.Read())
        {
          VRM2CustomControls.menuButton but = new VRM2CustomControls.menuButton();
          but.Text = myReader["username"].ToString();
          but.MouseEnter += buttons_MouseEnter;
          but.MouseLeave += buttons_MouseLeave;
          but.Click += userSettingName_click;
          userSettings.Children.Add(but);
        }
        userSettingsStack.Children.Add(userSettings);
        userSettingMenu.Content = userSettingsStack;

      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
        //.WriteLine(ex.ToString());
      }
    }

    private void userSettingName_click(object sender, RoutedEventArgs e)
    {
      subMenu_Click(sender, userSettingsWindow, "Settings For: ");
    }

    private void subMenu_Click(object sender, GroupBox grpBox, string heading = "")
    {
      VRM2CustomControls.menuButton menButton = (VRM2CustomControls.menuButton)sender;
      closeAll(false);

      StackPanel panel = (StackPanel)menButton.Parent;

      uncheckAll((StackPanel)panel.Parent);

      SolidColorBrush brsh = new SolidColorBrush(Color.FromRgb(21, 133, 181));
      menButton.Background = brsh;
      menButton.MouseEnter -= buttons_MouseEnter;
      menButton.MouseLeave -= buttons_MouseLeave;

      grpFromRight(grpBox, 0, 770);
      grpBox.Header = heading + menButton.Text;
    }

    private void username_settings_click(object sender, RoutedEventArgs e)
    {
      closeAll(true);
      grpFromRight(grpSettings, 0, 1027);
    }

    private void menuButton_Click_1(object sender, RoutedEventArgs e)
    {
      VRM2CustomControls.menuButton but = (VRM2CustomControls.menuButton)sender;

      subMenu_Click(sender, showWhich, but.Tag.ToString() + " - ");
    }

  }
}

